<?php 
namespace Admin\Controller;
use Think\Controller;

class CouponController extends AdminController
{
	public function index()
	{
		$cup = D('coupon');
		$list = $cup->select();
		$this->assign('list',$list);
		$this->display('Coupon/index');
	}

	public function add()
	{
		$this->display('Coupon/add');
	}

	public function doadd()
	{
		// var_dump($_POST);die;
		$cup = D('coupon');
		if (!$cup->create()) {
            $this->error($cup->getError());
        } else {
            if ($cup->add() > 0) {
                $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }
        }
	}

	public function del()
    {
        if (empty($_GET['id'])) {
            $this->error('没有id');
            exit;
        }
        $id = I('get.id/d');
    
        if (M('coupon')->delete($id) > 0) {
            $this->success('恭喜您!删除成功!');
        } else {
            $this->error('删除失败!');
        }
    }

	public function edit()
	{
		//接收ID
        $id = I('get.id/d');
        //查找单条数据
        $data = M('coupon')->find($id);
        // var_dump($data);
        $this->assign('data',$data);
		$this->display('Coupon/edit');
	}

	public function update()
    {
    	// var_dump($_POST);die;
        if (empty($_POST)) {
            $this->error('请填写数据!');
            exit;
        }
        D('coupon')->create();

        if (D('coupon')->save()) {
            $this->success('修改成功',U('index'));
        } else {
            $this->error('修改失败');
        }
    }

    public function status()
	{
		$id =  I('get.id/d');
		$data['status'] = $_GET['s'];
		// var_dump($data);die;
		$cup = M('coupon');
		$cup->where(array('id'=>$id))->save($data);
        $this->redirect('index');
	}



}